﻿Public Class frmDevolucionListado

    Dim controller As New ControllerSigeco.controller_compras()
    Dim controllerStock As New ControllerSigeco.controller_stock

    Private Sub frmComprasListado_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        fechaInicio.Value = DateTime.Now.AddDays(-30)
        fechaFin.Value = DateTime.Now
        oiml.CargarCombos(cboMotivo, "motivo", "idMotivoStock", controllerStock.get_motivos_stock_devolucion(oiml.dbConn, True))
        dgCompras.AutoGenerateColumns = False
        CargarRemisiones()

    End Sub

    Private _codProveedor As Integer = 0

    Public Property codProveedor() As Integer
        Get
            Return _codProveedor
        End Get
        Set(ByVal value As Integer)
            _codProveedor = value
        End Set
    End Property

    Private _codCliente As Integer = 0

    Public Property codCliente() As Integer
        Get
            Return _codCliente
        End Get
        Set(ByVal value As Integer)
            _codCliente = value
        End Set
    End Property

    Private m_frmRemisiones As frmDevolucion
    Public Property frmRemisiones() As frmDevolucion
        Get
            Return m_frmRemisiones
        End Get
        Set(ByVal value As frmDevolucion)
            m_frmRemisiones = value
        End Set
    End Property

    Private Sub CargarRemisiones()

        Dim dt As New DataTable
        dt = controller.get_Remision_All(oiml.dbConn, txtNroComprobante.Text, fechaInicio.Value, fechaFin.Value, "", _
                                    txtFactura.Text, cboMotivo.SelectedValue, cboEstado.SelectedValue, codCliente, codProveedor)
        dgCompras.DataSource = dt

    End Sub

    Private Sub btnCancelar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancelar.Click
        Me.Close()

    End Sub

    Private Sub btnEditar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEditar.Click
        Try
            If dgCompras.SelectedRows.Count > 0 Then
                Dim cod As New Integer
                cod = dgCompras.SelectedRows(0).Cells(0).Value

                If frmCompras Is Nothing Then

                    frmRemisiones = New frmDevolucion
                    frmRemisiones.codNotaRemision = cod

                    'If frmClientes.ShowDialog() = Windows.Forms.DialogResult.OK Then
                    frmRemisiones.ShowDialog()
                    CargarRemisiones()
                    frmRemisiones.Dispose()
                    frmRemisiones = Nothing
                    'End If
                Else
                    frmRemisiones.codNotaRemision = cod
                    frmRemisiones.ShowDialog()
                End If

            End If

        Catch ex As Exception
            MessageBox.Show("Error: " & ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)

        End Try

    End Sub

    Private Sub txtBuscar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtBuscar.Click
        CargarRemisiones()
    End Sub

    Private Sub txtProveedor_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs)
        If e.KeyCode = Keys.Enter Then
            CargarRemisiones()

        End If
    End Sub

    Private Sub dgCompras_DoubleClick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles dgCompras.DoubleClick
        btnEditar_Click(sender, e)
    End Sub

    Private Sub dgCompras_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles dgCompras.KeyDown
        If e.KeyCode = Keys.Enter Then
            btnEditar_Click(sender, e)
        End If

    End Sub

    Private Sub btnNuevo_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNuevo.Click
        frmRemisiones = New frmDevolucion()
        frmRemisiones.codNotaRemision = 0
        frmRemisiones.ShowDialog()
        CargarRemisiones()
    End Sub

    Private m_frmBusProveedor As frmProveedorBuscador
    Public Property frmBusProveedor() As frmProveedorBuscador
        Get
            Return m_frmBusProveedor
        End Get
        Set(ByVal value As frmProveedorBuscador)
            m_frmBusProveedor = value
        End Set
    End Property

    Private m_frmBusCliente As frmClienteBuscador
    Public Property frmBusCliente() As frmClienteBuscador
        Get
            Return m_frmBusCliente
        End Get
        Set(ByVal value As frmClienteBuscador)
            m_frmBusCliente = value
        End Set
    End Property

    Private Sub btnBuscProveedor_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBuscProveedor.Click
        frmBusProveedor = New frmProveedorBuscador
        frmBusProveedor.ShowDialog()
        txtProveedor.Text = frmBusProveedor.ProveedorDesc
        codProveedor = frmBusProveedor.codProveedor
        If codProveedor > 0 Then
            CargarRemisiones()
        End If
    End Sub

    Private Sub btnCancelProveedor_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancelProveedor.Click
        codProveedor = 0
        txtProveedor.Text = ""
        CargarRemisiones()
    End Sub

    Private Sub cboMotivo_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cboMotivo.SelectedIndexChanged
        If IsNumeric(cboMotivo.SelectedValue.ToString) Then
            CargarRemisiones()
        End If


    End Sub

    Private Sub btnBuscCliente_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBuscCliente.Click
        frmBusCliente = New frmClienteBuscador
        frmBusCliente.ShowDialog()
        txtCliente.Text = frmBusCliente.ClienteDesc
        codCliente = frmBusCliente.codCliente
        If codCliente > 0 Then
            CargarRemisiones()
        End If
    End Sub

End Class